package com.webchat.chat.web.chat;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.enums.FileType;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.ChatRecord;
import com.webchat.chat.service.ChatRecordMGService;
import com.webchat.chat.service.ChatRecordService;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.web.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/chat/record")
public class ChatRecordController extends BaseController {
//    @Resource
//    private ChatRecordService chatRecordService;

    @Resource
    private ChatRecordMGService chatRecordMGService;

    /**
     * 查询截止时间之前的对应房间的聊天记录
     * @param page
     * @param size
     * @param roomId 房间ID
     * @param time 截止时间
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size,
                       String roomId,
                       Date time) {
        //TODO 权限控制
        if(time == null){
            time = new Date();
        }
//        PageHelper.startPage(page, size);
//        List<ChatRecord> list = chatRecordService.findByRoomId(roomId, time);

        List<ChatRecord> list = chatRecordMGService.findByRoomIdAndTenantCode(roomId,page, size, time);

        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    /**
     * 根据页面信息查询聊天记录
     * @param page
     * @param size
     * @param sourceUserName  消息发送者
     * @param targetUserName
     * @param content
     * @param fileEnum       文件类型
     * @return
     */
    @PostMapping("/findChatRecordByInfo")
    public Result findChatRecordByInfo(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       @RequestParam(required = false) String sourceUserName,
                                       @RequestParam(required = false) String targetUserName,
                                       @RequestParam(required = false) String content,
                                       @RequestParam(required = false) String fileEnum
                                       ) {

        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setContent(content);
        chatRecord.setSourceUserName(sourceUserName);

        chatRecord.setTenantCode(ShiroUtil.getTenantCode());
        chatRecord.setTargetUserName(targetUserName);
        if (null != fileEnum && fileEnum.length()>0){
            chatRecord.setFileType(FileType.valueOf(fileEnum));
        }
//      PageHelper.startPage(page, size);
//      List<ChatRecord> list = chatRecordService.findChatRecordByInfo(chatRecord);
        List<ChatRecord> list = chatRecordMGService.findByConditions(chatRecord, page, size);


        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);

    }

}
